package com.shoppingmall.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 生成订单编号工具类
 */
public class GenarateOrderId {
    private static int num = 0;
    private static final int MIN_NUM=0;
    private static final int MAX_NUM=10000000;
    /**
     * 获得订单编号
     * @return
     */
    public synchronized static String getOrderId(){
        String timeStr = new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date());
        if(num>=MAX_NUM){
            num=MIN_NUM;
        }
        String orderId = timeStr + num;
        try {
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        num++;
        return  orderId;

    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        System.out.println(getOrderId());
                    }
                }
            }).start();
        }




    }
}
